package com.yc.rh.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yc.rh.dao.IAdminInfoDao;
import com.yc.rh.entities.AdminInfo;

/**
 * 后台管理员的业务层
 */
public class AdminInfoDaoImpl implements IAdminInfoDao {
	private SqlSession sqlSession;
	
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	
	@Override
	public int addAdminInfo(AdminInfo af) {
		return sqlSession.insert("addAdminInfo",af);
	}

	@Override
	public int delAdminInfo(String aids) {
		List<Integer> list=new ArrayList<Integer>();
		while(aids.contains(",")){
			Integer aid=Integer.valueOf(aids.substring(0, aids.indexOf(",")));
			aids=aids.substring(aids.indexOf(",")+1);
			list.add(aid);
		}
		list.add(Integer.valueOf(aids));
		System.out.println("长度   "+aids);
		return sqlSession.delete("delAdminInfo", list);
	}

	@Override
	public AdminInfo loginAdmin(String aid, String pwd) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("aid", aid);
		map.put("pwd", pwd);
		return sqlSession.selectOne("loginAdmin", map);
	}

	@Override
	public int updateAdmin(AdminInfo af) {
		return sqlSession.update("updateAdminInfo",af);
	}

	@Override
	public List<AdminInfo> find(Integer pageNo, Integer pageSize) {
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("min", (pageNo-1)*pageSize);
		map.put("max", pageNo*pageSize);
		return sqlSession.selectList("findAdminAll", map);
	}

	@Override
	public int getTotal() {
		return sqlSession.selectOne("getAdminTotal");
	}
}
